<?php

namespace app\common\api\controller;

use app\common\model\Member;
use think\Controller;
use think\Db;

class Relation extends Controller
{
    /**
     * 推荐关系
     */
    public function tRelation($tnumber)
    {
        $tuijian_info = Db('member')->where(array('usernumber' => $tnumber, 'status' => 1))->find();
        if (empty($tuijian_info)) {
            $data['code'] = -1000;
            return $data;//推荐人不存在
        }
        $tuijianid = $tuijian_info['uid'];

        if (empty($tuijian_info['tuijianids'])) {
            $tuijianids = ',' . $tuijianid . ',';//推荐人id组
        } else {
            $tuijianids = $tuijian_info['tuijianids'] . $tuijianid . ',';//推荐人id组
        }

        $tdeep = $tuijian_info['tdeep'] + 1;
        $data['tuijianid'] = $tuijianid;
        $data['tuijiannumber'] = $tnumber;
        $data['tuijianids'] = $tuijianids;
        $data['tdeep'] = $tdeep;
        $data['code'] = 1;
        return $data;
    }

    /**
     * 双规节点关系
     * @param $uid
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function pTwoRelation($uid)
    {
        $parent_info = Db('member')->where(array('uid' => $uid))->find();

        if ($parent_info['left_zone'] == 0) {
            $zone = 1;
        } else {
            if ($parent_info['right_zone'] == 0) {
                $zone = 2;
            } else {
                $this->error('无接点位！');
            }
        }

        $parentid = $parent_info['uid'];
        if (empty($parent_info['parentids'])) {
            $parents = ',' . $parentid . ',';//接点人id组
        } else {
            $parents = $parent_info['parentids'] . $parentid . ',';//接点人id组
        }

        if (!$parent_info['zid']) {
            $zid = 1;//接点人id组
        } else {
            $zid = $parent_info['zid'] . $zone;
        }

        $pdeep = $parent_info['pdeep'] + 1;

        $data['parentid'] = $parentid;
        $data['parentnumber'] = $parent_info['usernumber'];
        $data['parentids'] = $parents;
        $data['zid'] = $zid;
//        $data['parentareas'] = $parentareas;
        $data['pdeep'] = $pdeep;
        $data['zone'] = $zone;
        return $data;
    }


    public function pThreeRelation($uid)
    {
        $parent_info = Db('member')->where(array('uid' => $uid, 'status' => 1))->find();

        if ($parent_info['1_zone'] == 0) {
            $zone = 1;
        } elseif ($parent_info['2_zone'] == 0) {
            $zone = 2;
        } elseif ($parent_info['3_zone'] == 0) {
            $zone = 3;
        } else {
            $this->error('暂无接点位！');
        }

        $parentid = $parent_info['uid'];
        if (empty($parent_info['parentids'])) {
            $parents = ',' . $parentid . ',';//接点人id组
        } else {
            $parents = $parent_info['parentids'] . $parentid . ',';//接点人id组
        }

//        if (empty($parent_info['parentareas'])) {
//            $parentareas = ',' . $parentid . ':' . $zone . ',';//接点人id组
//        } else {
//            $parentareas = $parent_info['parentareas'] . $parentid . ':' . $zone . ',';//接点人id组
//        }

        $pdeep = $parent_info['pdeep'] + 1;
        $zid = $parent_info['zid'] . $zone;

        $data['parentid'] = $parentid;
        $data['parentnumber'] = $parent_info['usernumber'];
        $data['parentids'] = $parents;
//        $data['parentareas'] = $parentareas;
        $data['pdeep'] = $pdeep;
        $data['zone'] = $zone;
        $data['zid'] = $zid;
        return $data;
    }

    /**
     * 获取会员的接点位
     * @param $uid
     */
    public function getPosition($uid)
    {
        $user = Member::find($uid);
        if ($user->zone) {
            return;
        }
        if (!$user->tuijianid) {
            $user->zone = 1;
            $user->zids = '1';
            $user->save();
            return;
        }

        $tinfo = Member::find($user->tuijianid);

        if ($tinfo->znum < 3) {
            $this->getPPosition($user, $tinfo);
        } else {
            $cinfo = Member::where('parentid', $tinfo->uid)->where('zone', 3)->find();
            if ($cinfo->znum < 2) {
                $this->getPPosition($user, $cinfo);
            } else {
                $minPdeep = Member::where('znum', '<', 2)->where('status', 1)->where('parentids', 'like', $cinfo->parentids . $cinfo->uid . ',%')->where('zone', '<=', 2)->min('pdeep');
                $zids = Member::where('znum', '<', 2)->where('status', 1)->where('parentids', 'like', $cinfo->parentids . $cinfo->uid . '%')->where('pdeep', $minPdeep)->where('zone', '<=', 2)->column('zids');
                $minZids = min($zids);
                $pinfo = Member::where('zids', $minZids)->find();
                $this->getPPosition($user, $pinfo);
            }
        }
    }

    /**
     * 获取节点位置
     * @param $user
     * @param $pinfo
     */
    private function getPPosition($user, $pinfo)
    {
        $pinfo->znum += 1;
        $user->parentid = $pinfo->uid;
        $user->parentnumber = $pinfo->usernumber;
        $user->parentids = $pinfo->parentids ? $pinfo->parentids . $pinfo->uid . ',' : ',' . $pinfo->uid . ',';
        $user->pdeep = $pinfo->pdeep + 1;
        if ($pinfo->zone_1 == 0) {
            $user->zone = 1;
            $user->zids = $pinfo->zids . '1';
            $pinfo->zone_1 = 1;
        } elseif ($pinfo->zone_2 == 0) {
            $user->zone = 2;
            $user->zids = $pinfo->zids . '2';
            $pinfo->zone_2 = 1;
        } else {
            $user->zone = 3;
            $user->zids = $pinfo->zids . '3';
            $pinfo->zone_3 = 1;
        }

        $user->save();
        $pinfo->save();
    }
}